#!/bin/bash

TMPDIR=$(mktemp -d)


for DEBPATH in "$@"
do

    DEBPATH="${DEBPATH#file://}"
    DEBPATH=$(realpath $DEBPATH)
    OUTPATH=$(dirname "$DEBPATH")
    rm -rf $TMPDIR/unpack-dir
    mkdir -p "$TMPDIR/unpack-dir"
    cd "$TMPDIR/unpack-dir"
    ar -vx "$DEBPATH" 
    rm debian-binary
    tar -xvf data.tar*
    mkdir DEBIAN
    tar -xvf control.tar* -C ./DEBIAN
    rm data.tar* control.tar*
    fakeroot dpkg-deb -Z xz -b "$TMPDIR/unpack-dir/" "$OUTPATH"
    echo "Repack finished for $(dpkg-deb -f $DEBPATH Package). Remove tmp dir."
    rm -rf "$TMPDIR/unpack-dir"
    notify-send $(dpkg-deb -f "$DEBPATH" Package)重打包已完成 -i /usr/share/icons/uos-packaging-tools.png
done

echo "---------------------------------------------------------------------------"

